# -*- coding: utf-8 -*-
import os
import sys
import pytest
from flask import _request_ctx_stack

sys.path.insert(0, '..')
from app import create_app


@pytest.fixture(scope="session")
def app():
    app = create_app(os.environ.get('FLASK_ENV', 'default'))
    return app


@pytest.yield_fixture(scope="session")
def client(app):
    """A Flask test client. An instance of :class:`flask.testing.TestClient`
    by default.
    """
    with app.test_client() as client:
        yield client


@pytest.fixture(scope="session")
def client_class(request, client):
    """Uses to set a ``client`` class attribute to current Flask test client::

        @pytest.mark.usefixtures('client_class')
        class TestView:

            def login(self, email, password):
                credentials = {'email': email, 'password': password}
                return self.client.post(url_for('login'), data=credentials)

            def test_login(self):
                assert self.login('vital@example.com', 'pass').status_code == 200

    """
    if request.cls is not None:
        request.cls.client = client


@pytest.fixture(scope="session")
def config(app):
    """An application config."""
    return app.config


@pytest.fixture(scope="session")
def request_ctx(app):
    """The request context which contains all request relevant information,
    e.g. `session`, `g`, `flashes`, etc.
    """
    return _request_ctx_stack.top


@pytest.fixture(params=['application/json', 'text/html'], scope="session")
def mimetype(request):
    return request.param


@pytest.fixture(scope="session")
def accept_mimetype(mimetype):
    return [('Accept', mimetype)]


@pytest.fixture(scope="session")
def accept_json(request):
    return accept_mimetype('application/json')
